package com.zlf.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName DemoController
 * @Description TODO
 * @Author zlf
 * @Date 2021/5/19 13:36
 */
@RestController
public class DemoController {

    @Autowired
    private MyProcessor myProcessor;

    @GetMapping("/producer")
    public void producer(@RequestParam("msg") String msg) {
        Message<String> build = MessageBuilder.withPayload(msg).build();
        myProcessor.demoOutput().send(build);
    }

    @StreamListener(value = MyProcessor.DEMO_INPUT)
    @SendTo(value = MyProcessor.TEST_OUTPUT)
    public String receiveSendTo(String message) {
        System.out.println(String.format("demo接收到消息：%s", message));
        return "~~~~中转消息~~~~~~~" + message;
    }

    @StreamListener(value = MyProcessor.TEST_INPUT)
    public void receive2(String message) {
        System.out.println(String.format("test接收到消息：%s", message));
    }

}
